#include <iostream>

using namespace std;

void bubbleSort(int arr[], int arrLength)
{
    for (int i = 0; i < arrLength - 1; i++)
    {
        bool swapped = false;
        for (int j = 0; j < arrLength - i - 1; j++)
        {
            if (arr[j] > arr[j + 1])
            {
                // 交换元素
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                swapped = true;
            }
        }
        // 如果没有发生交换，说明数组已经有序，可以提前结束排序
        if (!swapped)
        {
            break;
        }
    }
}

int main()
{
    int arr[] = {1, 4, 3, 2, 5};
    int arrLength = sizeof(arr) / sizeof(arr[0]);

    cout << "Original array: ";
    for (int i = 0; i < arrLength; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;

    bubbleSort(arr, arrLength);

    cout << "Sorted array: ";
    for (int i = 0; i < arrLength; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;

    return 0;
}
